import java.util.Arrays;

public class TestHeap {
    public int[] elem;
    public int usedSize;
    public TestHeap() {
        this.elem = new int[10];
        this.usedSize = 0;
    }
    public void InitArray(int[] array) {

        for (int i = 0; i < array.length; i++) {
            if(isFull()) {
                elem = Arrays.copyOf(elem,2*elem.length);
            }
            this.elem[i] = array[i];
            usedSize++;
        }
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    public void createHeap() {
        for (int parent = (usedSize-1-1)/2; parent >= 0; parent--) {
            shiftDown(parent,usedSize);
        }
    }
    public void shiftDown(int parent,int usedSize) {
        int child = 2 * parent + 1;
        while(child < usedSize) {
            if(child + 1 < usedSize && elem[child] < elem[child+1]) {
                child = child + 1;
            }
            if(elem[parent] < elem[child]) {
                swap(elem,parent,child);
            }
            parent = child;
        }
    }
    public void swap(int[] elem,int parent,int child) {
        int tmp = elem[parent];
        elem[parent] = elem[child];
        elem[child] = tmp;
    }

    public void offer(int val) {
        if(isFull()) {
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        shiftUp(usedSize);
        usedSize++;
    }
    public void shiftUp(int child) {
        int parent = (child-1) / 2;
        while(parent >= 0) {
            if(elem[parent] < elem[child]) {
                swap(elem,parent,child);
                child  = parent;
                parent = (parent - 1) / 2;
            }else {
                break;
            }
        }
    }

    public void poll() {
        if(isEmpty()) {
            return ;
        }
        swap(elem,0,usedSize-1);
        usedSize--;
        shiftDown(0,usedSize);
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

}
